const $api = require('./../../utils/request').API;
var refashTime = null;
Page({
  data:{
    time: null ,
    baozhongDialog: false,
    addType:'',
    state:{},
    workingInfo:{},
    projectInfo:{},
    liuShui:{},
    xiazhongDialog:false,
    goodList:[],
  },

  shuaxin(){
    this.getJishi();
    this.getLiuShui();
  },

  jzbz(){
      wx.navigateTo({
        url: '/pages/jzbz/jzbz',
      })
  },



    changeProject(e){
      console.log( e.currentTarget.dataset)
      var id = e.currentTarget.dataset.id;
      var orderId = e.currentTarget.dataset.orderid;
      wx.navigateTo({
        url: `../projectList/projectList?projectId=${id}&orderId=${orderId}`,
      })
    },


  toMsg() {
    wx.navigateTo({
      url: './../msgcenter/msgcenter',
    })
  },

  baozhong(){
    this.setData({
      baozhongDialog:true
    })
  },


  diandan(e){
    var workingInfo = e.currentTarget.dataset.data;
    var roomId = workingInfo.roomId;
    var roomCode = workingInfo.roomCode;

    wx.navigateTo({
      url: `../project/project?roomId=${roomId}&roomCode=${roomCode}`,
    })
  },

  getLiuShui(){
    $api.getLiuShui({gongHao:wx.getStorageSync('gongHao')}).then(res=>{ 
      this.setData({
        liuShui:res.data
      })
      })
  },


getJishi(){
  $api.jishiStatus({jishiId:wx.getStorageSync('userId')}).then(res=>{ 
    this.setData({
      state:res.data
    })
    if (res.data.state == 2){
      $api.jishiWorkingInfo({jishiId:wx.getStorageSync('userId')}).then(result=>{
        this.setData({
          workingInfo:result.data,
        });
        this.setData({
          time:result.data.time * 60 *1000
        })
        this.getProjectInfo(result.data.projectId);
        this.getGoodList(result.data.roomId)

      });
    }
  }).catch(err=>{
    wx.hideLoading({
      fail:()=>{},
      success:()=>{},
      complete: (complete) => {}
    })
  })
},

getGoodList(roomId){
  $api.getGoods({ roomId }).then(res=>{
   
    if(res.data.length > 0){
      var jsGoodList = res.data.filter(item=>{
        if( item.jishiid == wx.getStorageSync('userId') ){
          return item;
        }
      })
      this.setData({
        goodList:jsGoodList
      })
    }


  })
},


getProjectInfo(id){
  $api.getProjectInfo(id).then(res=>{
    this.setData({
      projectInfo:res.tProject
    })
  })
},

addZhong(e){
  var id = e.currentTarget.dataset.id;
  var roomCode = e.currentTarget.dataset.roomcode;
  wx.navigateTo({
    url: `../addZhong/addZhong?project=${id}&roomCode=${roomCode}`,
  })
},

xiaZhong(){
  this.setData({
    xiazhongDialog:true
  })
},

xiazhongonConfirm(){
  var params = {
    fangJianHao:this.data.workingInfo.roomCode,
    gongHao:wx.getStorageSync('gongHao')
  }

    $api.xiaZhong(params).then(res=>{

      if(res.code == 200){
        wx.showToast({
          title: '下钟成功',
          icon:"success"
        })
        this.getJishi();
        this.getLiuShui();
      }else{
        wx.showToast({
          title: '下钟失败',
          icon:"error"
        })
      }


    })
},


finish(){
  console.log('over')
},

//报种
  onConfirm(e){
let params = {
  // fangJianHao: "",
  gongHao:wx.getStorageSync('gongHao')
}
    $api.shangZhong(params).then(res=>{
      wx.showToast({
        title:'调配成功',
        icon:'success',
        duration:1500
      })
     this.getJishi();
    this.getLiuShui();

    }).catch(err=>{
      wx.showToast({
        title: err.msg,
        icon:'none',
        duration:3000
      })
    })
  },

  addConConfirm(e){

  },


  onShow(){
    // refashTime  = setInterval(()=>{
    //   this.getJishi();
    //   this.getLiuShui();
    // },5000 )

    setTimeout(() => {
      this.getJishi();
      this.getLiuShui();
    }, 500);
  },

  onHide(){
    clearInterval(refashTime)
  },

  onUnload(){
    clearInterval(refashTime)
  },





})
